package net.csdn.business.discuss.common.context;

import net.csdn.business.discuss.common.context.model.AuthContext;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName AuthContextHolder
 * @Description TODO
 * @Author zhangyl
 * @Date 2023/8/9 16:48
 */
public class AuthContextHolder {
    private final static ThreadLocal<AuthContext> CONTEXT = new InheritableThreadLocal<>();

    public static AuthContext getContext() {
        return CONTEXT.get();
    }

    public static String getHuaweiToken() {
        if (ObjectUtils.isNotEmpty(getContext())) {
            return getContext().getHuaweiToken();
        }
        return StringUtils.EMPTY;
    }

    public static String getUid() {
        if (ObjectUtils.isNotEmpty(getContext())) {
            return getContext().getUid();
        }
        return StringUtils.EMPTY;
    }

    public static String getUsername() {
        if (ObjectUtils.isNotEmpty(getContext())) {
            return getContext().getUsername();
        }
        return StringUtils.EMPTY;
    }

    public static String getCodeArtsId() {
        if (ObjectUtils.isNotEmpty(getContext())) {
            return getContext().getCodeArtsId();
        }
        return StringUtils.EMPTY;
    }

    public static String getIamId() {
        if (ObjectUtils.isNotEmpty(getContext())) {
            return getContext().getIamId();
        }
        return StringUtils.EMPTY;
    }

    public static void setContext(AuthContext authContext) {
        CONTEXT.set(authContext);
    }

    public static void remove() {
        CONTEXT.remove();
    }
}